package hipt.com.dao.daoImpl;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import hipt.com.dao.AssetDao;
import hipt.com.model.Asset;

@Repository("assetDao")
public class AssetDaoImpl implements AssetDao {
	@Autowired
	private SessionFactory sessionFactory;

	@Override
	public void deleteAsset(Asset asset) {
		sessionFactory.getCurrentSession().delete(asset);
	}

	@Override
	public Asset getAssetByAssetId(Integer assetId) {
		return (Asset) sessionFactory.getCurrentSession().get(Asset.class, assetId);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Asset> getAssetByUserId(Long userId) {
		return sessionFactory.getCurrentSession().createQuery(
				"from Asset where userId=:userId").setParameter("userId",
				userId).list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Asset> listAssets() {
		return sessionFactory.getCurrentSession().createQuery("from Asset")
				.list();
	}

	@Override
	public void saveAsset(Asset asset) {
		sessionFactory.getCurrentSession().saveOrUpdate(asset);
	}

	@Override
	public void updateAsset(Asset asset) {
		sessionFactory
				.getCurrentSession()
				.createQuery(
						"UPDATE Asset SET name=:name ,description=:description, catId=:catId, status=:status where Id=:assetId")
				.setParameter("name", asset.getName()).setParameter(
						"description", asset.getDescription()).setParameter(
						"catId", asset.getCatId()).setParameter("status",
						asset.getStatus()).setParameter("Id", asset.getId())
				.executeUpdate();
	}

	
	@SuppressWarnings("unchecked")
	public List<Asset> getAssetByCatId(Long catId) {
		return sessionFactory.getCurrentSession().createQuery(
				"from Asset where catId=:catId").setParameter("catId",
						catId).list();
	}
}
